<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>

      * {
      padding: 0;
      margin: 0;
      border: none;
    }

    img {
      width: 200px;
      vertical-align: top;
    }

    #main {
      position: relative;
    }

    .box {
      float: left;
      padding: 15px 0 0 15px;
    }

    .pic {
      padding: 10px;
      border: 1px solid #ccc;
    }

  </style>
</head>

<body>
  <div id="main">
    <div class="box">
      <div class="pic">
        <img src="../images/5.jpg" alt="">
      </div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/1.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/2.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../imgs/day01.png" alt=""></div>
    </div>

    <div class="box">
      <div class="pic"><img src="../images/3.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../imgs/day02.png" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/4.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/5.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../imgs/day07.png" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/6.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/7.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../imgs/day08.png" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/8.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/1.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/2.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../imgs/day09.png" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/3.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../imgs/day10.png" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/4.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/5.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../imgs/day09drak.png" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/6.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/7.jpg" alt=""></div>
    </div>
    <div class="box">
      <div class="pic"><img src="../images/8.jpg" alt=""></div>
    </div>
  </div>

  <script>
   window.onload = function(){
       //在window加载完毕时调用此函数
       warterfall("main","box");
   }
   //定义waterfall函数
   function warterfall(parent,box){
       //将main下的所有class为box的元素取出来
       var oParent = document.getElementById(parent);
       var oBoxs = getByClass(oParent,box);
       //计算整个页面显示的列数(页面宽/box的宽);
       var oBoxW = oBoxs[0].offsetWidth;
       var cols = Math.floor(document.documentElement.clientWidth/oBoxW);
       //设置mian的宽度
       oParent.style.cssText = 'width:' + oBoxW * cols + 'px;margin:0 auto';
       var hArr = [];
       for(var i = 0; i < oBoxs.length;i++){
           if(i < cols){
               // 将前cols张图片的宽度记录到hArr数组中(第一行的高度)
               hArr.push(oBoxs[i].offsetHeight);
           }else{//从第二行开始就开始找最小的高度了，决定带插入图片该插入到哪里
               // 找到高度最小的值
               var minH = Math.min.apply(null,hArr);
               var index = getMinhIndex(hArr,minH);
               // 设置最小高度的图片的style为绝对定位，并设置top和left
               oBoxs[i].style.position = 'absolute';
               oBoxs[i].style.top = minH + 'px';
               oBoxs[i].style.left = oBoxW * index + 'px';
               hArr[index] += oBoxs[i].offsetHeight;
           }
       }
   }
   //根据class获取元素
   function getByClass(parent,clsName){
       var boxArr = [];//用来存储获取到的所有class为box的元素
       var oElements = parent.getElementsByTagName("*");
       for(var i = 0; i < oElements.length;i++){
           if(oElements[i].className == clsName){
               boxArr.push(oElements[i]);
           }
       }
       return boxArr;
   }
   //定义函数获取hArr数组中最小值的下标值
   function getMinhIndex(arr,val){
       for(var i in arr){
           if(arr[i] == val){
               return i;
           }
       }
   }
  </script>
</body>

</html>